#include "btreestore.h"

int main() {
    // Your own testing code here
    struct b_tree * helper = init_store( 5, 5);
    uint32_t encryption_key[4] = {1, 2, 3, 4};
    uint64_t nonce = 123;
    uint32_t plain[2] = {2, 4};
    uint32_t cipher[2];
    uint32_t p[2];
    encrypt_tea(plain, cipher, encryption_key);
    printf("cipher: %u  %u\n", cipher[0], cipher[1]);
    decrypt_tea(cipher, p, encryption_key);
    printf("plain: %u  %u\n", p[0], p[1]);
    printf("---------------\n");

    uint64_t plaintext[3] = {4, 5, 6};
    uint64_t ciphertext[3];
    uint64_t p2[3];
    encrypt_tea_ctr(plaintext, encryption_key, nonce, ciphertext, 3);
    printf("plain text: %llu  %llu %llu\n", ciphertext[0], ciphertext[1], ciphertext[2]);
    decrypt_tea_ctr(ciphertext, encryption_key, nonce, p2, 3);
    printf("cipher text: %llu  %llu %llu\n", p2[0], p2[1], p2[2]);
    printf("---------------\n");

    uint64_t plain_arr[3];
    uint64_t p_arr[3];
    for (int i = 1; i <= 8 ; i++) {
        plain_arr[0] = i;
        plain_arr[1] = i+1;
        plain_arr[2] = i+2;
        btree_insert(i, plain_arr, 24, encryption_key, 123,  helper);
    }
    uint64_t cipher_arr[3];
    struct info * info = malloc(sizeof (struct info));
    int res = btree_retrieve(8, info, helper);

    if(res == 0){
        memcpy(cipher_arr, info->data, 24);
        printf("cipherText: %llu, %llu, %llu\n", cipher_arr[0], cipher_arr[1], cipher_arr[2]);
        btree_decrypt(8, p_arr, helper);
        printf("plainText: %llu, %llu, %llu\n", p_arr[0], p_arr[1], p_arr[2]);
    }
    printf("---------------\n");

    btree_delete(5, helper);
    res = btree_retrieve(5, info, helper);
    if(res == 0){
        printf("find key:5\n");
    }else{
        printf("do not find key:5\n");
    }
    printf("---------------\n");

    struct node ** list = malloc(sizeof(struct node *) * 1000);
    int sum = btree_export(helper, list);
    printf("total node: %d\n", sum);

    for(int i = 0; i < sum; i++){
        struct node* node = list[i];
        printf("[");
        for (int j = 0; j < node->num_keys; j++) {
            printf("--%d-- ", node->keys[j]);
        }
        printf("]\n");
    }
    close_store(helper);

    return 0;
}
